

clear all
cap log close


*Graph style
sysdir set PERSONAL "W:\ado"
set scheme kailaScheme


global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe1 = "year"
global fe2 = "time year" 
global fe3 = "baseid time year" 
global fe_full = "baseid time year age_time" 
global cluster = "shnro"


capture program drop DiD
program define DiD
	args a b c d

	preserve
	
	use "${dataout}allmatches_allyears_matchpast5", clear
	
	
		
gen new_wp_crime=victim_sykstun_lag==defendant_sykstun_lag & defendant_sykstun_lag!=""
bys match_id1 year_event: ereplace new_wp_crime=max(new_wp_crime)
keep if new_wp_crime==1 
	
destring suspect_sex, replace 
destring plaintiff_sex, replace 
sort match_id1 year_event time shnro 
bys match_id1 year_event time: ereplace suspect_sex=max(suspect_sex)
bys match_id1 year_event time: ereplace plaintiff_sex=max(plaintiff_sex)

gen mf=(suspect_sex==1 & plaintiff_sex==2 ) 
gen mm=(suspect_sex==1 & plaintiff_sex==1)

keep if `d'==1 

merge m:1 sykstun year_event using  "${dataout}total_firm_topfemale"
drop if _merge ==2
drop _merge
	
rename share_female_top10  share_female_top
	
	* share heterogeneity 
su share_female_top if wp_crime==1 & time==0, detail
local med = r(p50)

g female_firm = (share_female_top>= `med' & share_female_top!=.) & wp_crime==1 & time==0

bys match_id1 year_event: ereplace female_firm=max(female_firm)

gen share_female_top_temp=share_female_top if wp_crime==1 & time==0 
bys match_id1 year_event: ereplace share_female_top_temp=max(share_female_top_temp)



sort shnro year_event time 
gen prior_earnings1=realAllEarn if time==-1 
gen prior_earnings2=realAllEarn if time==-2 
gen prior_earnings3=realAllEarn if time==-3

bysort shnro year_event: ereplace  prior_earnings1=mean( prior_earnings1)
bysort shnro year_event: ereplace  prior_earnings2=mean( prior_earnings2)
bysort shnro year_event: ereplace  prior_earnings3=mean( prior_earnings3)

egen prior_earnings=rmean (prior_earnings1 prior_earnings2 prior_earnings3)

gen share_earning=realAllEarn/prior_earnings

sum share_earning, d 
replace share_earning=10 if share_earning>10

gen baseyr=year_event

assert time >= -5 & time <= 5 

gen treat= wp_crime==1

gen age_time=age+time
*drop baseid

gen assault =(crimecode6=="210501")
replace assault=1-assault

gen treatPost= treat==1 & time>0
gen iteraction_occ=treatPost*manager_sub
gen iteraction_wagegap=treatPost*manager_sub2

gen iteraction_assault=treatPost*(assault)
gen iteraction_femaletop_dummy=treatPost*female_firm



* Generate treatment wave indentifier for each individual
egen baseid=group(shnro baseyr)


	capture erase "${results}DiD_perp_managerheter_`a'_`c'.tex"
	capture file close fh 

	file open fh using"${results}DiD_perp_managerheter_`a'_`c'.tex", write replace 
	

		file write fh ///
		"\begin{table}[H]" _n ///
		"\centering" _n ///
		"\begin{threeparttable}"_n ///
		"\caption{Heterogeneity Treatment Effect of Workplace Violence on Perps \label{tab:DiD`a'}}" ///
		"\centering{}" _n ///
		"\begin{tabular}{l l l l l }"_n ///
		"\toprule"_n ///
	     "  & (1) & (2)  & (3)   & (4)   \\"  _n ///
		 "  & `a' &`a' & `a'  & `a'\\"  _n ///
		 "\midrule " _n
		 
		 
	
			* Main regression will all fixed effects
	reghdfe `a' iteraction_femaletop_dummy treatPost , absorb($fe_full)  cluster($cluster)
	local betaMain2 = string(_b[iteraction_femaletop_dummy], "%10.3fc")
	local seMain2 = string(_se[iteraction_femaletop_dummy], "%10.3fc")
	local betaMain_treat2 = string(_b[treatPost], "%10.3fc")
	local seMain_treat2 = string(_se[treatPost], "%10.3fc")
	
	
	local obs2 = string((e(N)), "%10.3fc")
	sum `a' if wp_crime == 0 
	local mean_control2=string( r(mean) , "%10.3fc")
	


	
	file write fh "Treatment*FemaleManager & `betaMain2'   & `betaMain4' & `betaMain6'   & `betaMain8' & \\"   _n
	file write fh "				    &   (`seMain2')   &  (`seMain4')  &  (`seMain6')   &  (`seMain8')  &    \\"   _n
	
	
	file write fh "Treatment	  & `betaMain_treat2'  &  `betaMain_treat4'   & `betaMain_treat6'  &  `betaMain_treat8'   \\"   _n
	file write fh "				 & (`seMain_treat2')  	   & (`seMain_treat4')  & (`seMain_treat6')  	   & (`seMain_treat8')  \\"   _n
	

	
	
	file write fh "\\"   _n

	
	
	file write fh "Year fixed effects   	 &  \checked				  & \checked			&	\checked		&	\checked			\\ " _n 
	file write fh "Time since crime fixed effects 		&  \checked				  & \checked			&	\checked		&	\checked			\\ " _n
	file write fh "Individual fixed effects   	&  \checked				  & \checked			&	\checked		&	\checked				\\ " _n 
	file write fh "Age by time since the event   	&  \checked				  & \checked			&	\checked		&	\checked		\\ " _n 
	
	file write fh "\midrule " _n
	file write fh "N. of Obs. 		  	 		  &  `obs2' 		  & `obs4' 	&  `obs6' & `obs8'    \\ " _n

	file write fh "Non-victims mean  	  		  &  `mean_control2' 		&	`mean_control4'			&  `mean_control6' &  `mean_control8' \\ " _n

	
	file write fh "\bottomrule" _n ///
				  "\end{tabular}" _n ///
				  "\end{threeparttable}" _n ///
				  "\end{table}"
	file close fh 
	restore
	*}
end


DiD "employed" "realAllEarn" "PanelAmf" "mf"
DiD "employed" "realAllEarn" "PanelBmm" "mm"


